<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/7/18
 * Time: 13:44
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\business;

use app\common\lib\Status;
use app\common\lib\Tree;
use app\common\model\mysql\Category as ModelObj;
use app\common\validate\Category as ValidateObj;
use think\Exception;
use think\facade\Log;

class Category extends BusBase
{
    protected ?ModelObj $modelObj = null;
    protected ?ValidateObj $validateObj = null;

    public function __construct()
    {
        $this->modelObj = new ModelObj();
        $this->validateObj = new ValidateObj();
    }

    public function getIndexPage(array $map = [], int $limit = 10)
    {
        //查询第一个分类
        $where = [
            ['pid', '=', 0]
        ];
        $data = array_merge($map, $where);

        $firstCate = $this->modelObj->getIndexPage($data, $limit, 'sort desc,id desc');
        //获取所有不等于0的数据
        $map[] = [
            ['pid', '<>', 0]
        ];
        //dump($map);exit();
        $otherCate = $this->modelObj->getBaseByWherePage($map);
        $resData['data'] = Tree::getInstance()->TreeCate(array_merge($firstCate['data'], $otherCate), 0, 1, "treeList");
        $resData['total'] = $firstCate['total'];
        return $resData;
    }

    /**
     * 添加操作
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function add(array $data = []): bool
    {
        if (empty($data)) {
            throw new Exception("数据不完整");
        }

        //验证
        if (!$this->validateObj->scene('add')->check($data)) {
            throw new Exception($this->validateObj->getError());
        }
        //验证标题是否存在
        try {
            $ret = $this->modelObj->getCategoryByIsTitle($data['title']);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("查询失败");
        }
        if ($ret) {
            throw new Exception("分类名称已经存在");
        }
        //添加入库
        try {
            $res = $this->modelObj->add($data);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("入库失败");
        }
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getCateTree(string $field = 'id as value,title as name,pid,id'): array
    {
        //获取菜单
        try {
            $cate = $this->modelObj->getCateTree($field);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("查询错误");
        }
        if (!empty($cate)) {
            return Tree::getInstance()->TreeCate($cate);
        } else {
            return [];
        }
    }

    /**
     * @param int $pid
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getCategoryByPid(int $pid = 0, string $field = '*'): array
    {
        $orderBy = "sort desc ,id desc";
        try {
            $result = $this->modelObj->getCategoryByPid($pid, $field, $orderBy);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("查询错误");
        }
        return $result;
    }

}